<?php

function isUserValid($username, $password){
	$user_id = "";
	$statement = "SELECT user_id FROM user WHERE username=$username AND password_md5=$password";
	$result = mysql_query($statement);

	$user_id = mysql_fetch_array($result);
	$user_id = $user_id['user_id'];
	
	if($user_id == null){
		return 0;
	}else{
		return $user_id['user_id'];
	}
}

function registerUser($user_information){
	// Validation
	if($user_information[0] == "" ||
	   $user_information[2] == "" ||
	   $user_information[3] == "" ||
	   $user_information[4] == "" ||
	   $user_information[5] == "" ||
	   $user_information[6] == "" ||
	   $user_information[7] == "" ){
		return 2;
	}else{
		$username = $user_information[0];
		$password = $user_information[1];
		$name 	  = $user_information[2];
		$surname  = $user_information[0];
		$street   = $user_information[4];
		$zip 	  = $user_information[5];
		$country  = $user_information[6];
	
		if($password != $user_information[7]){
			return 3;
		}
	}
	
	// Check if Username exists
	$statement = "SELECT user_id FROM user WHERE username=$username";
	$result = mysql_query($statement);
	$user_exists = mysql_fetch_array($result);
	if($user_exists['user_id'] == null){
		$user_exists = false;
	}else{
		$user_exists = true;
	}
	
	// Save user to db
	if($user_exists){
		return 4;
	}else{
		$statement = "INSERT INTO user (name, surname, street, ZIP, country, username, password_md5, role_role_id) ".
					 "VALUES ('$name', '$surname', '$street', '$zip', '$country', '$username', '$password', '1')";
		
		$result = mysql_query($statement);
	}
}
?>